/**
 * Classe responsável para criar o world, inimigos e o player
 * esta classe tera o que todas as outras fases do game terao
 */
 
import core.*;
import core.gameelement.*;
import core.gameelement.world.*;
import core.gameelement.agent.*;

import flash.geom.*;
import util.*;

import rocknrollracing.gameelement.*;
import rocknrollracing.gameelement.agent.airocknrollracing.*;
import rocknrollracing.gameelement.agent.perspective.*;
import rocknrollracing.gameelement.agent.*;
 
//extendes de Phase só para o PhaseFactory não reclamar... 
class rocknrollracing.phase.RocknRollRacingPhase extends Phase
{
	private var world:RocknRollRacingWorld;
	private var canvasTopDown:MovieClip;	
	private var gameElements:Array; 
	
	public function RocknRollRacingPhase()
	{
		super();
		gameElements = new Array();		
	}
	
	public function updateLogic():Void
	{
		world.notifyObservers(GameManager.UPDATE_LOGIC);
		
		for(var i:Number = 0; i < getGameElements().length; i++)
			getGameElements()[i].notifyObservers(GameManager.UPDATE_LOGIC);
		  
	}
	public function updateScreen():Void
	{
		world.notifyObservers(GameManager.UPDATE_SCREEN);
		
		for(var i:Number = 0; i < getGameElements().length; i++)
			getGameElements()[i].notifyObservers(GameManager.UPDATE_SCREEN);		
		  
	}
	
	public function start():Void
	{
		canvasTopDown = _root.createEmptyMovieClip("canvastopdown",1);
		canvasTopDown._xscale = 100;
		canvasTopDown._yscale = 100;
	}
	
	public function startEspecifyPhase():Void
	{
		
	}
	
	public function createWorld(id:String):Void
	{
		world = new RocknRollRacingWorld(id);
		world.addObserver(new WorldTopDownGameElementView(canvasTopDown, world.getModel()));
	}
	
	
	public function createPlayer(id:String, position:Point):Void
	{
		var player:RocknRollRacingAgent = new RocknRollRacingAgent(id,position);
		player.getModel().setPosition(position);
		player.addObserver(new RocknRollRacingAgentTopDownViewPixel(world.getWorldTopDownCanvasGameElement(), player.getModel()));
		player.setSolid(true);
		// nao preciso setar o dono da inteligencia pois quanto seto a inteligencia automaticamente
		// o metodo se seta como dono		
		player.setIntelligence(new PlayerIntelligence());		
		//AgentModel(player.getModel()).setLife(3);
		addGameElements(player);		
	}
	
	/*
	public function createPlayer(id:String, position:Point,activeState,direction:Number):Void
	{	
		var tile:Tile = world.getTileByPosition(position);		
		player = new Agent(id);
		player.addObserver(new View(player.getModel(),world.getWorldCanvas()));
		TileableModel(player.getModel()).setZIndex(1);
		player.setTile(world.getTileByPosition(position));		
		player.addObserver(new IsometricView(player.getModel(),world.getWorldCanvasIsometric()));						 
		player.setIntelligence(new PacmanAvatarIntelligence());		
		AgentModel(player.getModel()).setActiveState(activeState);
		AgentModel(player.getModel()).setDirection(direction);		
		player.setSolid(true);
		AgentModel(player.getModel()).setLife(100);		
	}
	*/
	
	public function createWall(id:String, position:Point):Void
	{		
		var wall:WallPixel = new WallPixel(id);
		wall.getModel().setPosition(position);
		wall.addObserver (new GameElementView(world.getWorldTopDownCanvasGameElement(), wall.getModel()));
		wall.setSolid(true);
		addGameElements(wall);
	}
	
	public function createItem(id:String, position:Point):Void
	{
		// ja seta o graphic do item no model
		var item:ItemPixel = new ItemPixel(id);
		item.getModel().setPosition(position);		
		item.addObserver(new GameElementView(world.getWorldTopDownCanvasGameElement(), item.getModel()));
		item.setSolid(false);
		addGameElements(item);
		
	}
	
	public function getGameElements():Array
	{
		return gameElements;
	}

	public function addGameElements(gameElement:GameElement):Void
	{
		gameElements.push(gameElement);
	}
	
	
	public function toString(Void):String
	{
		return "rocknrollracing.phase.RocknRollRacingPhase";
	}	
}